﻿//***********************************************************************
// * Module:   MsjBaSqblbayzHandler
// * 作者:  XWG
// * Modified: 2013-5-29
// * Purpose: Definition of the Class MsjBaSqblbayzHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using XWG.JWBST.JWBSTBLL;
using JWBSTAPP.CommonClass;
using XWG.Framework;

namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjBaSqblbayzHandler 的摘要说明
    /// </summary>
    public class MsjBaSqblbayzHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                MsjBaSqblbayz mMsjBaSqblbayz = new MsjBaSqblbayz();
                mMsjBaSqblbayz.Sqid = Guid.NewGuid().ToString(); //主键
                mMsjBaSqblbayz.Xm = context.Request.Form["texXm"]; //姓名
                mMsjBaSqblbayz.Xb = context.Request.Form["radXb"]; //性别(字典)
                mMsjBaSqblbayz.Zzmm = context.Request.Form["texZzmm"]; //政治面貌(字典)
                mMsjBaSqblbayz.Cym = context.Request.Form["texCym"]; //曾用名
                mMsjBaSqblbayz.Mz = context.Request.Form["texMz"]; //民族(字典)
                mMsjBaSqblbayz.Hyzk = context.Request.Form["texHyzk"]; //婚姻状况(字典)
                mMsjBaSqblbayz.Whcd = context.Request.Form["texWhcd"]; //文化程度(字典)
                mMsjBaSqblbayz.Xx = context.Request.Form["texXx"]; //血型(字典)
                mMsjBaSqblbayz.Byzk = context.Request.Form["texByzk"]; //兵役状况
                mMsjBaSqblbayz.Csrq = context.Request.Form["texCsrq"]; //出生日期
                mMsjBaSqblbayz.Sg = Convert.ToInt16(context.Request.Form["texSg"]); //身高
                mMsjBaSqblbayz.Jkzk = context.Request.Form["texJkzk"]; //健康状况(字典)
                mMsjBaSqblbayz.Sfzh = context.Request.Form["texSfzh"]; //身份证号
                mMsjBaSqblbayz.Dh = context.Request.Form["texDh"]; //电话
                mMsjBaSqblbayz.Sj = context.Request.Form["texSj"]; //手机
                mMsjBaSqblbayz.Jg = context.Request.Form["texJg"]; //籍贯(字典)
                mMsjBaSqblbayz.Hjdxz = context.Request.Form["texHjdxz"]; //户籍地详址
                mMsjBaSqblbayz.Hjdxq = context.Request.Form["texHjdxq"]; //户籍地辖区
                mMsjBaSqblbayz.Xzdxz = context.Request.Form["texXzdxz"]; //现住地详址
                mMsjBaSqblbayz.Xzdxq = context.Request.Form["texXzdxq"]; //现住地辖区
                mMsjBaSqblbayz.Gajgsh = context.Request.Form["texGajgsh"]; //公安机关审核
                mMsjBaSqblbayz.Kscj = float.Parse(context.Request.Form["texKscj"]); //考试成绩
                mMsjBaSqblbayz.Gzbm = context.Request.Form["texGzbm"]; //工作部门
                mMsjBaSqblbayz.Baygw = context.Request.Form["texBaygw"]; //保安员岗位
                mMsjBaSqblbayz.Rzsj = context.Request.Form["texRzsj"]; //入职时间
                mMsjBaSqblbayz.Lzrq = context.Request.Form["texLzrq"]; //离职日期
                mMsjBaSqblbayz.Lzyy = context.Request.Form["texLzyy"]; //离职原因
                mMsjBaSqblbayz.Jzzzh = context.Request.Form["texJzzzh"]; //居(暂)住证号
                mMsjBaSqblbayz.Sfqdldht = context.Request.Form["radSfqdldht"]; //是否签订劳动合同(1是  0否)
                mMsjBaSqblbayz.Sfcjsb = context.Request.Form["radSfcjsb"]; //是否参加社保(1是  0否)
                mMsjBaSqblbayz.Baybh = context.Request.Form["texBaybh"]; //保安员编号
                mMsjBaSqblbayz.Fzjg = context.Request.Form["texFzjg"]; //发证机关
                mMsjBaSqblbayz.Fzrq = context.Request.Form["texFzrq"]; //发证日期
                mMsjBaSqblbayz.Bayzydj = context.Request.Form["texBayzydj"]; //保安员职业等级
                mMsjBaSqblbayz.Zydjfzjg = context.Request.Form["texZydjfzjg"]; //职业等级发证机关
                mMsjBaSqblbayz.Zydjfzrq = context.Request.Form["texZydjfzrq"]; //职业等级发证日期
                mMsjBaSqblbayz.Jszh = context.Request.Form["texJszh"]; //驾驶证号
                mMsjBaSqblbayz.Zjcx = context.Request.Form["texZjcx"]; //准驾车型
                mMsjBaSqblbayz.Jszfzrq = context.Request.Form["texJszfzrq"]; //驾驶证发证日期
                mMsjBaSqblbayz.Cqzbh = context.Request.Form["texCqzbh"]; //公务用枪持枪证编号
                mMsjBaSqblbayz.Cqzqx = context.Request.Form["texCqzqx"]; //公务用枪持枪证枪型
                mMsjBaSqblbayz.Cqzfzrq = context.Request.Form["texCqzfzrq"]; //持枪证发证日期
                mMsjBaSqblbayz.Cqzyxqx = context.Request.Form["texCqzyxqx"]; //持枪证有效期限
                mMsjBaSqblbayz.Cqzfzjg = context.Request.Form["texCqzfzjg"]; //持枪证发证机关
                mMsjBaSqblbayz.Zwbh = context.Request.Form["texZwbh"]; //指纹编号
                mMsjBaSqblbayz.Baybh2 = context.Request.Form["texBaybh2"]; //保安员编号2
                mMsjBaSqblbayz.Rylx = context.Request.Form["texRylx"]; //人员类型
                mMsjBaSqblbayz.Gsbh = context.Request.Form["texGsbh"]; //公司编号
                mMsjBaSqblbayz.Gsmc = context.Request.Form["texGsmc"]; //公司名称
                mMsjBaSqblbayz.Slrqm = context.Request.Form["texSlrqm"]; //申领人签名
                mMsjBaSqblbayz.Qmrq = context.Request.Form["texQmrq"]; //签名日期
                if (!string.IsNullOrEmpty(context.Request.Form["H_File"]))
                {
                    mMsjBaSqblbayz.Zp = new ImageToByte().ToByte("/uploadfiles/" + context.Request.Form["H_File"]); //照片
                }

                mMsjBaSqblbayz.Cjr = context.Session["UserID"].ToString(); //创建人(人员ID)
                //mMsjBaSqblbayz.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID) 
                mMsjBaSqblbayz.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                mMsjBaSqblbayz.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjBaSqblbayzBLL.Insert(mMsjBaSqblbayz, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            string paramKey = context.Request.Params["id"];
            try
            {
                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }

                MsjBaSqblbayz mMsjBaSqblbayz = new MsjBaSqblbayz();
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mResultModel = MsjBaSqblbayzBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel));  //序列化后返回
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjBaSqblbayz mMsjBaSqblbayz = new MsjBaSqblbayz();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjBaSqblbayz = MsjBaSqblbayzBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjBaSqblbayz != null)
                {
                    mMsjBaSqblbayz.Xm = context.Request.Form["texXm"]; //姓名
                    mMsjBaSqblbayz.Xb = context.Request.Form["radXb"]; //性别(字典)
                    mMsjBaSqblbayz.Zzmm = context.Request.Form["texZzmm"]; //政治面貌(字典)
                    mMsjBaSqblbayz.Cym = context.Request.Form["texCym"]; //曾用名
                    mMsjBaSqblbayz.Mz = context.Request.Form["texMz"]; //民族(字典)
                    mMsjBaSqblbayz.Hyzk = context.Request.Form["texHyzk"]; //婚姻状况(字典)
                    mMsjBaSqblbayz.Whcd = context.Request.Form["texWhcd"]; //文化程度(字典)
                    mMsjBaSqblbayz.Xx = context.Request.Form["texXx"]; //血型(字典)
                    mMsjBaSqblbayz.Byzk = context.Request.Form["texByzk"]; //兵役状况
                    mMsjBaSqblbayz.Csrq = context.Request.Form["texCsrq"]; //出生日期
                    mMsjBaSqblbayz.Sg = Convert.ToInt16(context.Request.Form["texSg"]); //身高
                    mMsjBaSqblbayz.Jkzk = context.Request.Form["texJkzk"]; //健康状况(字典)
                    mMsjBaSqblbayz.Sfzh = context.Request.Form["texSfzh"]; //身份证号
                    mMsjBaSqblbayz.Dh = context.Request.Form["texDh"]; //电话
                    mMsjBaSqblbayz.Sj = context.Request.Form["texSj"]; //手机
                    mMsjBaSqblbayz.Jg = context.Request.Form["texJg"]; //籍贯(字典)
                    mMsjBaSqblbayz.Hjdxz = context.Request.Form["texHjdxz"]; //户籍地详址
                    mMsjBaSqblbayz.Hjdxq = context.Request.Form["texHjdxq"]; //户籍地辖区
                    mMsjBaSqblbayz.Xzdxz = context.Request.Form["texXzdxz"]; //现住地详址
                    mMsjBaSqblbayz.Xzdxq = context.Request.Form["texXzdxq"]; //现住地辖区
                    mMsjBaSqblbayz.Gajgsh = context.Request.Form["texGajgsh"]; //公安机关审核
                    mMsjBaSqblbayz.Kscj = float.Parse(context.Request.Form["texKscj"]); //考试成绩
                    mMsjBaSqblbayz.Gzbm = context.Request.Form["texGzbm"]; //工作部门
                    mMsjBaSqblbayz.Baygw = context.Request.Form["texBaygw"]; //保安员岗位
                    mMsjBaSqblbayz.Rzsj = context.Request.Form["texRzsj"]; //入职时间
                    mMsjBaSqblbayz.Lzrq = context.Request.Form["texLzrq"]; //离职日期
                    mMsjBaSqblbayz.Lzyy = context.Request.Form["texLzyy"]; //离职原因
                    mMsjBaSqblbayz.Jzzzh = context.Request.Form["texJzzzh"]; //居(暂)住证号
                    mMsjBaSqblbayz.Sfqdldht = context.Request.Form["radSfqdldht"]; //是否签订劳动合同(1是  0否)
                    mMsjBaSqblbayz.Sfcjsb = context.Request.Form["radSfcjsb"]; //是否参加社保(1是  0否)
                    mMsjBaSqblbayz.Baybh = context.Request.Form["texBaybh"]; //保安员编号
                    mMsjBaSqblbayz.Fzjg = context.Request.Form["texFzjg"]; //发证机关
                    mMsjBaSqblbayz.Fzrq = context.Request.Form["texFzrq"]; //发证日期
                    mMsjBaSqblbayz.Bayzydj = context.Request.Form["texBayzydj"]; //保安员职业等级
                    mMsjBaSqblbayz.Zydjfzjg = context.Request.Form["texZydjfzjg"]; //职业等级发证机关
                    mMsjBaSqblbayz.Zydjfzrq = context.Request.Form["texZydjfzrq"]; //职业等级发证日期
                    mMsjBaSqblbayz.Jszh = context.Request.Form["texJszh"]; //驾驶证号
                    mMsjBaSqblbayz.Zjcx = context.Request.Form["texZjcx"]; //准驾车型
                    mMsjBaSqblbayz.Jszfzrq = context.Request.Form["texJszfzrq"]; //驾驶证发证日期
                    mMsjBaSqblbayz.Cqzbh = context.Request.Form["texCqzbh"]; //公务用枪持枪证编号
                    mMsjBaSqblbayz.Cqzqx = context.Request.Form["texCqzqx"]; //公务用枪持枪证枪型
                    mMsjBaSqblbayz.Cqzfzrq = context.Request.Form["texCqzfzrq"]; //持枪证发证日期
                    mMsjBaSqblbayz.Cqzyxqx = context.Request.Form["texCqzyxqx"]; //持枪证有效期限
                    mMsjBaSqblbayz.Cqzfzjg = context.Request.Form["texCqzfzjg"]; //持枪证发证机关
                    mMsjBaSqblbayz.Zwbh = context.Request.Form["texZwbh"]; //指纹编号
                    mMsjBaSqblbayz.Baybh2 = context.Request.Form["texBaybh2"]; //保安员编号2
                    mMsjBaSqblbayz.Rylx = context.Request.Form["texRylx"]; //人员类型
                    mMsjBaSqblbayz.Gsbh = context.Request.Form["texGsbh"]; //公司编号
                    mMsjBaSqblbayz.Gsmc = context.Request.Form["texGsmc"]; //公司名称
                    mMsjBaSqblbayz.Slrqm = context.Request.Form["texSlrqm"]; //申领人签名
                    mMsjBaSqblbayz.Qmrq = context.Request.Form["texQmrq"]; //签名日期
                    if (!string.IsNullOrEmpty(context.Request.Form["H_File"]))
                    {
                        mMsjBaSqblbayz.Zp = new ImageToByte().ToByte("/uploadfiles/" + context.Request.Form["H_File"]); //照片
                    }
                    mMsjBaSqblbayz.Xgsj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //创建时间(2013-05-28 09:13:01)
                    mMsjBaSqblbayz.Xgr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjBaSqblbayz.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID) 
                    mMsjBaSqblbayz.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                    mMsjBaSqblbayz.Movesign = "0"; //是否传输（1已，0未）

                    mResultModel = MsjBaSqblbayzBLL.Update(mMsjBaSqblbayz, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;
                }

                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = string.Empty;
            try
            {
                MsjBaSqblbayz mMsjBaSqblbayz = new MsjBaSqblbayz(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjBaSqblbayz = MsjBaSqblbayzBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjBaSqblbayz != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjBaSqblbayz);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }

            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string sqlWhere = " enabled=1";
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
            {
                return;
            }
            string mc = context.Request.Form["texXm"];
            string sj = context.Request.Form["texSj"];
            string xb = context.Request.Form["radXb"];

            SqlConditionList sqllist = new SqlConditionList();

            if (!string.IsNullOrEmpty(mc)) ////姓名
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaSqblbayzConst.Columns.Xm, '%' + mc + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(sj)) ////法定代表人
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaSqblbayzConst.Columns.Sj, '%' + sj + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(xb)) ////姓名
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjBaSqblbayzConst.Columns.Xb, xb), SqlLogicOper.AND_LOGIC);
            }

            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where" + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }

            DataTable dtMsjBaSqblbayz = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            dtMsjBaSqblbayz = q.GetRecordByPage("*", sqlWhere, string.Empty, intPageSize, intPageIndex, MsjBaSqblbayzConst.TableName, MsjBaSqblbayzConst.FieldKey, string.Empty).Tables[0];
            if (dtMsjBaSqblbayz != null && dtMsjBaSqblbayz.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjBaSqblbayz.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjBaSqblbayz.Columns[i].Caption;
                    list.Add(hm);
                }

                tm.Header = list;
                tm.data = dtMsjBaSqblbayz;
                tm.totalCount = Convert.ToString((int)q.RowCount);
            }

            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }

        protected void QueryGtRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";

            QueryInfo info = new QueryInfo();
            QueryByPage page = new QueryByPage();
            info.FieldKey = MsjBaSqblbayzConst.FieldKey;
            info.PageIndex = 1;
            info.PageSize = 0;
            info.QTableName = MsjBaSqblbayzConst.TableName;
            info.SelectFields = "*";

            info.Where = string.Empty;
            DataSet ds = page.GetRecordByPage(info);
            DataTable table = new DataTable();

            if (ds.Tables.Count > 0)
            {
                table = page.GetRecordByPage(info).Tables[0];
            }
            string json = JsonConvert.SerializeObject(table);
            context.Response.Write(json);
        }
        #endregion
    }
}


